<template>
  <div class="refueling_list">
    <c-title :hide="false" text="油站导航"></c-title>
    <template v-if="storeList.length > 0">
      <div class="storeListBox" :class="{ sli: storeId == item.id }" @click="tapStoreLis(item.id)" v-for="item in storeList" :key="item.id">
        <div class="storeListBox_l">
          <div class="title">{{ item.store_name }}</div>
          <div class="address">{{ item.full_address }}</div>
          <div class="tagBox" v-if="item.text_info && item.text_info.nav_label_arr.length > 0">
            <div class="tag" v-for="(items, index) in item.text_info.nav_label_arr" :key="index">{{ items }}</div>
          </div>
        </div>
        <div class="storeListBox_r" @click="goToAdress(item)">
          <div class="iconfontBox">
            <i class="iconfont icon-zx_map_local"></i>
          </div>
          <div class="address_txt">{{ disCheck(item) }}{{ item.unit }}</div>
        </div>
      </div>
    </template>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      storeList: [],
      storeId: "",
      point: {},
      city: "",

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    if (this.$route.params.store_id) {
      this.storeId = this.$route.params.store_id;
    }
    this.init();
    this.getData();
  },
  computed: {
    disCheck(item) {
      return function(item) {
        if (!item.distance) return;
        return item.distance.toFixed(2);
      };
    }
  },
  methods: {
    init() {
      this.page = 1;
      this.total_page = 1;
      this.isLoadMore = true;
      this.storeList = [];
    },
    tapStoreLis(id) {
      this.storeId = id;
      this.$router.push(this.fun.getUrl("refueling_payment", { store_id: this.storeId }));
    },
    goToAdress(item) {
      let point = this.fun.bd_decrypt(item.longitude, item.latitude);
      this.fun.goToWXAdress(point, item.store_name, item.full_address);
    },
    async getData() {
      let res = await this.fun.getLocation();
      this.point = res.point;
      this.city = res.city;
      let json = {
        lng: this.point.lng,
        lat: this.point.lat,
        city: this.city,
        name: this.storeNameKey
      };
      let { data, msg, result } = await $http.post("plugin.store-oil-station.frontend.unpaid-order.store", json, "");
      if (result) {
        let store = data.data;
        this.storeList = store;
        this.total_page = data.last_page;
        this.isLoadMore = true;
        if (!this.$route.params.store_id) {
          let cind;
          for (let i = 0; i < store.length; i++) {
            if (store[i].is_default == 1) {
              cind = i;
              break;
            }
          }
          if (cind) {
            this.storeId = store[cind].id;
          } else {
            this.storeId = store[0].id;
          }
        }
      } else {
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          lng: this.point.lng,
          lat: this.point.lat,
          city: this.city,
          name: this.storeNameKey,
          page: this.page
        };
        let { data, msg, result } = await $http.post("plugin.store-oil-station.frontend.unpaid-order.store", json, "");
        this.isLoadMore = true;
        if (result) {
          this.storeList = this.storeList.concat(data.data);
        } else {
          this.$toast(msg);
          this.page = this.page - 1;
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.sli {
  border: 0.06rem solid #f15353;
}
.refueling_list {
  box-sizing: border-box;
  padding-top: 0.5rem;
  padding-bottom: 2rem;
  .storeListBox {
    padding: 0.88rem 0.63rem;
    box-sizing: border-box;
    background: #ffffff;
    border-radius: 0.5rem;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin: 0 0.75rem;
    margin-bottom: 0.63rem;
    .storeListBox_l {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      .title {
        font-weight: bold;
        font-size: 1rem;
        color: #00001c;
      }
      .address {
        font-size: 0.81rem;
        color: #00001c;
        margin-top: 0.84rem;
        text-align: left;
      }
      .detailBox {
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        align-items: center;
        margin-top: 0.84rem;
        .detail {
          font-size: 0.75rem;
          color: #00001c;
          margin-right: 0.66rem;
        }
        .detail:last-child {
          margin-right: 0;
        }
      }
      .tagBox {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
        margin-top: 0.84rem;
        .tag {
          font-size: 0.63rem;
          color: #3b3b4a;
          line-height: 0.75rem;
          padding: 0.16rem 0.31rem;
          box-sizing: border-box;
          border-radius: 0.25rem;
          border: 0.03rem solid #aaaab3;
          margin-right: 0.31rem;
        }
        .tag:last-child {
          margin-right: 0;
        }
      }
    }
    .storeListBox_r {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-left: 0.5rem;
      .iconfontBox {
        width: 1.5rem;
        height: 1.5rem;
        background: #f5f5f5;
        border-radius: 0.5rem;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 0.5rem;
        .icon-zx_map_local {
          font-size: 0.8rem;
        }
      }
      .address_txt {
        font-size: 0.75rem;
        color: #3b3b4a;
      }
    }
  }
}
</style>
